/* -----------------
* brief
* 1. this is code gen by tools
 */

package service

import (
	"errors"
	wxcard "github.com/chanxuehong/wechat/mp/card"
	"github.com/chanxuehong/wechat/mp/core"
	"github.com/mjiulee/lego"
)
import . "saas_sys_base/modules/wechat/models"

type WxCardUserCardService struct {
	// add your model here
	tbWxCardUserCard            WxCardUserCard
	wechatService               WechatService
	wxCardUserMemberCardService WxCardUserMemberCardService
}

func (t *WxCardUserCardService) ListWxCardUserCard(keyword string, page, psize int) ([]map[string]string, int64, error) {
	// 业务查询
	queryMap := map[string]interface{}{
		"table":  map[string]string{"table": "tb_wx_card_user_card", "alias": "t1"},
		"fields": []string{"t1.*"},
		"join":   []map[string]string{
			// map[string]string{"table": "tb_xx as t2", "on": "t2.xid = t1.id"},
		},
		"where": []lego.QMapWhereField{
			//{"t1.if_del", "=", 0, "AND", false},
		},
		"search": map[string]string{
			//"t1.id": req.Keyword,
		},
		"orders": []string{"t1.id asc"},
		"page": map[string]int{
			"page":  page,
			"psize": psize,
		},
	}
	return lego.QueryListByMap(queryMap)
}

/* 根据id获取记录
 * -----------------
 */
func (t *WxCardUserCardService) GetWxCardUserCardById(id int64) *WxCardUserCard {
	return t.tbWxCardUserCard.RecodeById(id)
}

/* 添加
 * -----------------
 */
func (t *WxCardUserCardService) SaveWxCardUserCard(item2add *WxCardUserCard) bool {
	return t.tbWxCardUserCard.AddRecode(item2add)
}

/* 删除(硬删除)*/
func (t *WxCardUserCardService) DelWxCardUserCardById(id int64) bool {
	return t.tbWxCardUserCard.DelRecodeById(id)
}

/* 更新
 * -----------------*/
func (t *WxCardUserCardService) UpdateWxCardUserCard(rc *WxCardUserCard) bool {
	return t.tbWxCardUserCard.UpdateRecode(rc)
}

func (t *WxCardUserCardService) CardGetCardEventHandler(weappid string, mixedMsg *core.MixedMsg) (*WxCardUserCard, error) {
	msg := wxcard.GetUserGetCardEvent(mixedMsg)
	//weappid := msg.AppId
	cardid := msg.CardId
	cardcode := msg.UserCardCode

	s := lego.NewDBSession()
	defer s.Close()
	_ = s.Begin()

	usercard := t.tbWxCardUserCard.ReocodeByWeappIdCardIdAndCardCodeWithsession(s.GetSession(), weappid, cardid, cardcode)
	if usercard != nil {
		s.Rollback()
		return nil, nil
	}

	rc := NewWxCardUserCardByGetCardEvent(weappid, msg)
	isSuccess := t.tbWxCardUserCard.AddRecodeWithsession(s.GetSession(), rc)
	if !isSuccess {
		s.Rollback()
		return nil, errors.New("tbWxCardUserCard.AddRecodeWithsession failed")
	}

	s.Commit()

	return rc, nil
}

func (t *WxCardUserCardService) UserDelCardEventHandler(weappid string, mixedMsg *core.MixedMsg) error {
	msg := wxcard.GetUserDelCardEvent(mixedMsg)
	//weappid := msg.AppId
	cardid := msg.CardId
	cardcode := msg.UserCardCode

	isSuccess := t.tbWxCardUserCard.SDelRecodeByWeappIdCardIdAndCardCode(weappid, cardid, cardcode)
	if !isSuccess {
		return errors.New("tbWxCardUserCard.SDelRecodeByWeappIdCardIdAndCardCode failed")
	}

	return nil
}

func (t *WxCardUserCardService) UserConsumeCardEventHandler(weappid string, mixedMsg *core.MixedMsg) error {
	msg := wxcard.GetUserConsumeCardEvent(mixedMsg)
	//weappid := msg.AppId
	cardid := msg.CardId
	cardcode := msg.UserCardCode

	usercard := new(WxCardUserCard)
	usercard.SetConsume(msg)
	isSuccess := t.tbWxCardUserCard.UpdateRecodeByWeappIdCardIdAndCardCode(usercard, weappid, cardid, cardcode)
	if !isSuccess {
		return errors.New("tbWxCardUserCard.UpdateRecodeByWeappIdCardIdAndCardCode failed")
	}

	return nil
}

func (t *WxCardUserCardService) UserViewCardEventHandler(weappid string, mixedMsg *core.MixedMsg) error {
	// TODO统计计数以后再考虑
	return nil
}

func (t *WxCardUserCardService) UserEnterSessionEventHandler(weappid string, mixedMsg *core.MixedMsg) error {
	// TODO以后再考虑
	return nil
}

func (t *WxCardUserCardService) SubmitMemberCardUserInfoEventHandler(weappid string, mixedMsg *core.MixedMsg) error {
	t.wxCardUserMemberCardService.SubmitMemberCardUserInfoEventHandler(weappid, mixedMsg)
	return nil
}
